﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace VeryCodes.Controls.Web
{
    public class ExtCalendar : Calendar
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.PrevMonthText = AppendPrevYear() + base.PrevMonthText;
            base.NextMonthText = base.NextMonthText + AppendNextYear();
            base.Render(writer);
        }

        /// <summary>
        /// Called when a match to the previous month link is found
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private string AppendPrevYear()
        {
            // append the previous year link to the front of the previous month link
            return "<a href=\"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "PrevYearClick") + "\" id=\"" + this.ClientID + "_btnPrevYear\"><<</a>" + "&nbsp";
        }

        /// <summary>
        /// Called when a match to the next month link is found
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private string AppendNextYear()
        {
            // append the previous year link to the front of the previous month link
            return "&nbsp" + "<a href=\"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "NextYearClick") + "\" id=\"" + this.ClientID + "_btnPrevYear\">>></a>";
        }

        protected override void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument == "PrevYearClick")
            {
                btnPrevYear_Click(this, new EventArgs());
            }
            else if (eventArgument == "NextYearClick")
            {
                btnNextYear_Click(this, new EventArgs());
            }
            else
            {
                base.RaisePostBackEvent(eventArgument);
            }
        }

        protected void btnPrevYear_Click(object sender, EventArgs e)
        {
            if (base.VisibleDate != null && base.VisibleDate.Year > 1)
            {
                base.VisibleDate = base.VisibleDate.AddYears(-1);
            }
        }

        protected void btnNextYear_Click(object sender, EventArgs e)
        {
            if (base.VisibleDate != null)
            {
                base.VisibleDate = base.VisibleDate.AddYears(1);
            }
        }
    }
}
